package com.aiYouPms.admin.controller;

import com.aiYouPms.admin.dto.AdminUserCreateParams;
import com.aiYouPms.admin.dto.AdminUserUpdateParams;
import com.aiYouPms.admin.util.SecurityUtil;
import com.aiYouPms.common.api.HttpResult;
import com.aiYouPms.common.api.PageResult;
import com.aiYouPms.common.service.AdminUserService;
import com.aiYouPms.common.model.AdminUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Controller
@ResponseBody
@RequestMapping("/adminUser")

public class AdminUserController {

    private final AdminUserService adminUserService;

    @Autowired
    public AdminUserController(AdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    /**
     * 根据ID获取管理员用户
     * @param id 管理员用户ID
     * @return 管理员用户信息
     */
    @GetMapping("/info")
    public HttpResult<AdminUser> getAdminUserById(@RequestParam Integer id) {
        AdminUser adminUser = adminUserService.findById(id);
        if (adminUser == null) {
            return HttpResult.error("管理员用户不存在");
        }
        return HttpResult.success(adminUser);
    }

    /**
     * 根据用户名获取管理员用户
     * @param username 用户名
     * @return 管理员用户信息
     */
    @GetMapping("/infoByUsername")
    public HttpResult<AdminUser> getAdminUserByUsername(@RequestParam String username) {
        AdminUser adminUser = adminUserService.findByUsername(username);
        if (adminUser == null) {
            return HttpResult.error("管理员用户不存在");
        }
        return HttpResult.success(adminUser);
    }

    /**
     * 更新管理员用户
     * @param id 管理员用户ID
     * @param adminUserUpdateParams 更新的管理员用户信息
     * @return 更新结果
     */
    @PostMapping("/update")
    public HttpResult<AdminUser> updateAdminUser(@RequestParam (value = "id") Integer id,  @Valid @RequestBody AdminUserUpdateParams adminUserUpdateParams) {
        AdminUser existingUser = adminUserService.findById(id);
        if (existingUser == null) {
            return HttpResult.error("管理员不存在");
        }
        BeanUtils.copyProperties(adminUserUpdateParams,existingUser);
        adminUserService.update(id,existingUser);
        return HttpResult.success("管理员更新成功", existingUser);
    }



    /**
     * 分页查询管理员用户
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 分页结果
     */
    @GetMapping("/search")
    public HttpResult<PageResult<AdminUser>> getAdminUserPage(
            @RequestParam(value = "username",defaultValue ="", required = false) String username,
            @RequestParam(value = "nickname", required = false) String nickname,
            @RequestParam(value = "mobile",defaultValue ="", required = false) String mobile,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        AdminUser adminUser = new AdminUser();
        adminUser.setUsername(username);
        adminUser.setNickname(nickname);
        adminUser.setMobile(mobile);
        adminUser.setStatus(status);

        PageHelper.startPage(pageNum, pageSize);
        List<AdminUser> adminUsers = adminUserService.findAll(adminUser);
        PageInfo<AdminUser> pageInfo = new PageInfo<>(adminUsers);

        PageResult<AdminUser> pageResult = PageResult.of(
                pageInfo.getList(),
                pageInfo.getTotal(),
                pageNum,
                pageSize);

        return HttpResult.success(pageResult);
    }

    /**
     * 创建新的管理员用户（注册）
     * @param adminUserCreateParams 管理员用户信息
     * @return 创建结果
     */
    @PostMapping("create")
    public HttpResult<AdminUser> create(@Valid @RequestBody AdminUserCreateParams adminUserCreateParams) {
        AdminUser existingUser = adminUserService.findByUsername(adminUserCreateParams.getUsername());
        if (existingUser != null) {
            return HttpResult.error("用户名已存在");
        }
        AdminUser adminUser = new AdminUser();
        BeanUtils.copyProperties(adminUserCreateParams,adminUser);
        // 创建用户
        AdminUser createdUser = adminUserService.create(adminUser);
        return HttpResult.success("管理员用户创建成功", createdUser);
    }

    @GetMapping("/profile")
    public HttpResult<AdminUser> profile() {
        Integer currentUserId = SecurityUtil.getCurrentUserId();
        if (currentUserId == null) {
            return HttpResult.error("用户未登录");
        }
        
        AdminUser adminUser = adminUserService.findById(currentUserId);
        if (adminUser == null) {
            return HttpResult.error("用户不存在");
        }
        
        return HttpResult.success(adminUser);
    }

}